package hos.ui.item;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;

import java.util.Map;

import hos.java.color.ColorConfig;
import hos.java.util.AnyUtils;
import hos.java.util.MapUtils;
import hos.ui.R;
import hos.ui.edit.IconEditText;
import hos.ui.edit.IconTextView;
import hos.ui.time.DateText;
import hos.ui.time.TimeView;

/**
 * <p>Title: ItemTextLayout </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/6/9 17:45
 */
public class ItemTextLayout extends ItemTitleLayout {

    private IconTextView iconTextView;

    public ItemTextLayout(Context context) {
        this(context, null);
    }

    public ItemTextLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemTextLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void initAttributeSet(Context context, AttributeSet attrs, int defStyleAttr) {
        @SuppressWarnings("resource") TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.ItemTextLayout);

        try {

            String text = typedArray.getString(R.styleable.ItemTextLayout_contentText);
            String hint = typedArray.getString(R.styleable.ItemTextLayout_contentHint);
            int textColor = typedArray.getColor(
                    R.styleable.ItemTextLayout_contentColor,
                    ColorConfig.black87
            );
            int colorHint = typedArray.getColor(
                    R.styleable.ItemTextLayout_contentColorHint,
                    ColorConfig.grey.shade200()
            );
            int contentLines = typedArray.getInteger(R.styleable.ItemTextLayout_contentLines, Integer.MAX_VALUE);
            int contentMinLines = typedArray.getInteger(R.styleable.ItemEditLayout_contentMinLines, Integer.MAX_VALUE);
            //px
            int textSize = typedArray.getDimensionPixelSize(
                    R.styleable.ItemTextLayout_contentSize,
                    applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
            );
            boolean singleLine = typedArray.getBoolean(R.styleable.ItemTextLayout_contentSingleLine, false);

            Drawable contentBackground = typedArray.getDrawable(
                    R.styleable.ItemTextLayout_contentBackground
            );

            int textType = typedArray.getInteger(R.styleable.ItemTextLayout_textType, 0);
            if (textType == 1) {
                // date
                iconTextView = genDateText();
                boolean isFull = typedArray.getBoolean(
                        R.styleable.ItemTextLayout_isFull,
                        true
                );
                String format = typedArray.getString(R.styleable.ItemTextLayout_dateFormat);
                if (TextUtils.isEmpty(format)) {
                    format = "yyyy-MM-dd HH:mm:ss";
                }
                TimeView dateText = (TimeView) iconTextView;
                dateText.setFull(isFull);
                dateText.setFormat(format);
            } else {
                iconTextView = genIconTextView();
            }

            iconTextView.setTextColor(textColor);
            iconTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            iconTextView.setHint(hint);
            iconTextView.setHintTextColor(colorHint);
            iconTextView.setText(text);
            if (contentLines != Integer.MAX_VALUE) {
                iconTextView.setLines(contentLines);
            }
            if (contentMinLines != Integer.MAX_VALUE) {
                iconTextView.setMinLines(contentMinLines);
            }
            if (singleLine) {
                iconTextView.setLines(1);
                iconTextView.setSingleLine();
            }
            if (contentBackground == null) {
                int backgroundType = typedArray.getInteger(R.styleable.ItemTextLayout_backgroundType, -1);
                IconEditText.setBackground(iconTextView, backgroundType);
            } else {
                iconTextView.setBackground(contentBackground);
            }
//            if (contentBackground == null) {
//                int contentBackgroundColor = typedArray.getColor(
//                        R.styleable.ItemTextLayout_contentBackground,
//                        -1
//                );
//                if (contentBackgroundColor == -1) {
//                    int contentBackgroundRes = typedArray.getResourceId(
//                            R.styleable.ItemTextLayout_contentBackground,
//                            -1
//                    );
//                    if (contentBackgroundRes != -1) {
//                        iconTextView.setBackgroundResource(contentBackgroundRes);
//                    }
//                } else {
//                    iconTextView.setBackgroundColor(contentBackgroundColor);
//                }
//            } else {
//                iconTextView.setBackground(contentBackground);
//            }

            int cdId = typedArray.getResourceId(R.styleable.ItemTextLayout_suffixIcon, -1);
            if (cdId == -1) {
                cdId = typedArray.getInt(R.styleable.ItemTextLayout_suffixIcon, -1);
            }
            if (cdId != -1) {
                iconTextView.setSuffixIcon(cdId);
            } else {
                Drawable drawable = typedArray.getDrawable(R.styleable.ItemTextLayout_suffixIcon);
                if (drawable != null) {
                    iconTextView.setSuffixIcon(drawable);
                }
            }

            // 前缀按钮
            int prefixId = typedArray.getResourceId(R.styleable.ItemTextLayout_prefixIcon, -1);
            if (prefixId == -1) {
                prefixId = typedArray.getInt(R.styleable.ItemTextLayout_prefixIcon, -1);
            }
            if (prefixId != -1) {
                iconTextView.setPrefixIcon(prefixId);
            } else {
                Drawable drawable = typedArray.getDrawable(R.styleable.ItemTextLayout_prefixIcon);
                if (drawable != null) {
                    iconTextView.setPrefixIcon(drawable);
                }
            }

            int orientation = getOrientation();
            if (orientation == HORIZONTAL) {
                int childGravity = typedArray.getInt(R.styleable.ItemTextLayout_childGravity, Gravity.LEFT | Gravity.CENTER_VERTICAL);
                iconTextView.setGravity(childGravity);
                LayoutParams layoutParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
                layoutParams.weight = 1;
                iconTextView.setLayoutParams(layoutParams);
            } else {
                int childGravity = typedArray.getInt(R.styleable.ItemTextLayout_childGravity, Gravity.LEFT | Gravity.TOP);
                iconTextView.setGravity(childGravity);
                iconTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
            int childMinHeight = typedArray.getDimensionPixelOffset(R.styleable.ItemRadioLayout_childMinHeight, dp2px(48));
            iconTextView.setMinimumHeight(childMinHeight);
            addView(iconTextView);
        } finally {
            typedArray.recycle();
        }
    }

    @Override
    public void setContent(Map<String, Object> map, String key) {
        setKey(key);
        if (iconTextView == null) {
            return;
        }
        if (!map.containsKey(key)) {
            return;
        }
        iconTextView.setText(MapUtils.empty(map, key));
    }

    @Override
    public void setContent(CharSequence content) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setText(AnyUtils.empty(content));
    }

    public String getText() {
        if (iconTextView != null) {
            return AnyUtils.empty(iconTextView.getText());
        }
        return "";
    }

    @Override
    public void clear() {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setText("");
    }

    @Override
    public void clearListener() {
        super.clearListener();
        if (iconTextView != null) {
            iconTextView.clearListener();
        }
    }

    public void clearHint() {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setHint("");
    }

    public TimeView getDateText() {
        if (iconTextView instanceof TimeView) {
            return (TimeView) iconTextView;
        }
        return null;
    }

    public IconTextView getIconTextView() {
        return iconTextView;
    }

    protected TimeView genDateText() {
        return new DateText(getContext());
    }

    protected IconTextView genIconTextView() {
        return new IconTextView(getContext());
    }

    public void setOnContentClickListener(OnClickListener l) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setOnClickListener(l);
    }

    public void setSuffixIcon(Drawable src) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setSuffixIcon(src);
    }

    public void setSuffixIcon(int src) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setSuffixIcon(src);
    }

    public void setPrefixIcon(Drawable src) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setPrefixIcon(src);
    }

    public void setPrefixIcon(int src) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setPrefixIcon(src);
    }

    public void setOnSuffixIconClickListener(OnClickListener iconClickListener) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setOnSuffixIconClickListener(iconClickListener);
    }

    public void setOnPrefixIconClickListener(OnClickListener iconClickListener) {
        if (iconTextView == null) {
            return;
        }
        iconTextView.setOnPrefixIconClickListener(iconClickListener);
    }

    public void setContentEnabled(boolean enabled) {
        if (iconTextView != null) {
            iconTextView.setEnabled(enabled);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        if (iconTextView != null) {
            iconTextView.setEnabled(enabled);
        }
        super.setEnabled(enabled);
    }

    public void setClickableEnabled(boolean clickable) {
        if (iconTextView != null) {
            iconTextView.setClickable(clickable);
        }
    }

    @Override
    public void setClickable(boolean clickable) {
        if (iconTextView != null) {
            iconTextView.setClickable(clickable);
        }
        super.setClickable(clickable);
    }

    public void loadLonlat() {
        if (iconTextView == null) {
            return;
        }
        iconTextView.loadLonlat();
    }
}
